<?php
class BookmarkController extends CController{
	public function actionIndex(){
		$url = trim(urldecode(NC::_g('url','')));
		if(!$url) exit;
		$title = trim(urldecode(NC::_g('title','')));

		//登录处理
		if(Yii::app()->user->isGuest){
			Yii::app()->user->returnUrl = "http://dev.dadipedia.com/bookmark?url=".urlencode($url)."&l=login";
			echo 'document.location="http://dev.dadipedia.com/account/login"';
			Yii::app()->end();
		}
		//添加数据处理
			$model = new Links('getContent');
			$validateUrlUnique = News::validateUrlUnique($url);
			$result = 1;//失败
			if($validateUrlUnique !== null){
				$result = 3;//该网址已经存在!，作顶的操作
				$news = News::model()->find('link_url=:link_url',array(':link_url'=>$url));
				$news_id = $news->news_id;
				if($news_id){
					//$actions = $this->loadModel($news_id);
					$actions = Actions::model()->exists('news_id=:news_id and user_id=:user_id',array(':news_id'=>$news_id,':user_id'=>Yii::app()->user->id));
					if(!$actions){
						if(Actions::addActions($news_id,'digg')){
							//更新nc_news表
							News::model()->updateCounters(array('digg_num'=>1),'news_id=:news_id',array(':news_id'=>$news_id));
							//更新用户表
							User::model()->updateCounters(array('share_num'=>1),'user_id=:user_id',array(':user_id'=>Yii::app()->user->id));
							$result = 3;//成功
						}
						else
							$result = 2;//已经分享过
					}else
						$result = 2;//已经分享过
				}else{
					$result = 1;//失败
				}
			}else{
				$curl = new Curl();
				$crawl = $curl->getData($url);
				$news = new News;
				$news->link_url = $url;
				$news->site_title = $crawl['title'];
				$news->news_desc = $crawl['description'];
				if($news->createNews($news)){
					$news_id = $news->primaryKey;
					CacheFile::cacheThumb($news_id,$news->link_url);
					$result = 3;//成功
				}
			}
		//输出处理
		if(NC::_g('l','') == 'login'){//如果是首次登录直接跳转到原网页
			Yii::app()->user->returnUrl = '/';//重置返回地址
			$this->redirect($url);
		}else{//已经登录则输出js
			require_once('bookmark'.$result.'.js');
		}
	}
}

?>
